function [Ar,Br,Cr,Nr,Hr] = GSI(A,B,C,N,H,V,Ar,Br,Cr,Nr,Hr)
%GSI 算法主体函数
%   此处显示详细说明
% 设置循环终止的条件
max_iterations = 8;
tolerance = 1e-2;
iteration = 0;

%对H预先处理
H1 = sparseMatrixToTensor(H);
H_sy = my_spten_symmetry(H1);
H = sptenmat(H_sy,1);
H2 = sptenmat(H_sy,2);
v1 = V;
while iteration < max_iterations
%     [V1, ~] = eigs(Ar);
%     Nr = V1' * Nr * V1;
%     %Hr = V1' * Hr * kron(V1,V1);
%     Br = V1' * Br;
%     Cr = Cr * V1;
    Hr1 = sparseMatrixToTensor(Hr);
    Hr_sy = my_spten_symmetry(Hr1);
    Hr = sptenmat(Hr_sy,1);
    Hr2 = sptenmat(Hr_sy,2);

    % 初始化上一步的特征值
    prev_eigenvalues_A = Ar;
    %combineh = sumH(H, Hr, V);
    v1 = computeV(A, Ar, B, Br, N, Nr, double(H), double(Hr), v1);        
        
    combineh2 = sumH(2*double(H2), double(Hr2), v1);
    w1 = Vec1(A', Ar', -C', Cr', N', Nr', combineh2);
    
    V = orth(v1);
    W = orth(w1);
    
    K = (W'*V)\W';
    Ar = K*A*V;
    Nr = K*N*V;
    Br = K*B;
    Cr = C*V;
    Hr = K*double(H)*kron(V,V);
    % 计算矩阵 Ar 的特征值
    eigenvalues_A = Ar;
    
    % 比较特征值差的绝对值是否小于容差
    if max(eig(eigenvalues_A - prev_eigenvalues_A)) < tolerance
        disp('特征值相等，结束循环。');
        break;  % 结束循环
    end
    
    k = max(eig(eigenvalues_A - prev_eigenvalues_A));
    % 更新迭代次数
    iteration = iteration + 1;
    
    disp(['当前算法已完成' num2str(iteration) '次,算法中Ar的残差为：' num2str(k)]);
    disp('\n');
end

% 如果循环达到最大迭代次数仍未终止，则输出警告信息
if iteration == max_iterations
    disp(['警告：未能满足特征值相等的条件:' num2str(tolerance)]);
end